package com.exam.exceptions;
import com.exam.entity.ApiResult;
import com.exam.util.ApiResultHandler;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
/**
 * 统一异常拦截处理
 * 可以针对异常的类型进行捕获，然后返回json信息到前端
 */
@ControllerAdvice
public class GraceExceptionHandler {

    @ExceptionHandler(MyCustomException.class)
    @ResponseBody
    public ApiResult returnMyException(MyCustomException e) {
        e.printStackTrace();
        return ApiResultHandler.buildApiResult(400, e.getMessage(),null);
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    @ResponseBody
    public ApiResult returnMaxUploadSize(MaxUploadSizeExceededException e) {
//        e.printStackTrace();
        //文件大小超出限制
        return ApiResultHandler.buildApiResult(400, "文件过大",null);
    }

}
